package com.blogspot.wanyancan.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Uploadit extends Composite {
	final FormPanel form = new FormPanel();
	final TextBox tb = new TextBox();
	final ListBox lb = new ListBox();
	final DockPanel mainpanel = new DockPanel();
	
	public Uploadit() {
		initWidget(mainpanel);
		form.setAction(GWT.getModuleBaseURL()+"/upload");

		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		
		VerticalPanel panel = new VerticalPanel();
		form.setWidget(panel);

		tb.setName("textboxEle");
		panel.add(tb);

		lb.setName("listBoxEle");
		lb.addItem("foo", "fooValue");
		lb.addItem("bar", "barValue");
		lb.addItem("baz", "bazValue");
		panel.add(lb);
		// test fileupload widget
		FileUpload upload = new FileUpload();
		upload.setName("uploadEle");
		panel.add(upload);

		panel.add(new Button("Submit", new ClickListener() {
			public void onClick(Widget sender) {
				form.submit();
			}
		}));

		// dealing with form results
		form.addFormHandler(new FormHandler() {
			public void onSubmit(FormSubmitEvent event) {
				if (tb.getText().length() <= 0) {
					Window.alert("text box can't be empty");
					event.setCancelled(true);
				}

			}

			public void onSubmitComplete(FormSubmitCompleteEvent event) {
				Window.alert("shit it!!");
				Window.alert(event.getResults()+event.toString());
			}
		});
		mainpanel.add(form, DockPanel.NORTH);

	}
}
